<%--
  Created by IntelliJ IDEA.
  User: qinsiyuan
  Date: 2021/12/9
  Time: 13:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<%--<script type="text/javascript">
    function test(){
        //发送异步请求
        //1.创建XMLHttpRequest对象
        var xmlHttp;
        if (window.XMLHttpRequest){
            //非IE，那么创建XMLHttpRequest对象
            xmlHttp = new XMLHttpRequest();
        }else if (window.ActiveXObject){
            //IE,那么创建ActiveXObject
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //2.打开链接
        var uname = document.getElementById("uname").value;
        //get方式
        xmlHttp.open("get","/testuname?uname="+uname,true);
        //post方式
        //xmlHttp.open("post","/testuname",true);
        //指定回调函数
        xmlHttp.onreadystatechange = function (){
            //3.1判断状态
            if (xmlHttp.readyState==4){
                //接受返回的数据
                var responseText = xmlHttp.responseText;//返回文本
                document.getElementById("rs").innerText=responseText;//写入
            }
        }
        //4.get发送数据
        xmlHttp.send();
        //5.post发送数据
        // var uname= document.getElementsByName("userName")[0].value;
        // var upass= document.getElementsByName("userPass")[0].value ;
        // var params = "userName=" + uname+ "&userPass=" +upass+ "&time=" +Math.random();//增加time随机参数，防⽌读取缓存
        // xmlHttp.setRequestHeader("Content-type","application/x-www-formurlencoded;charset=UTF-8");
        // xmlHttp.send(params);
    }
</script>--%>
<script type="text/javascript">
    $(function(){
        $("#uname").blur(function(){
            //1.获得value值
            var uname = $(this).val();
            //2.发送请求
            /*$.get("/testuname","uname="+uname,function(rs){
                $("#rs").html(rs);
             });*/
            $.post("/testuname","uname="+uname,function(rs){
                //转换成可以处理的js
                //rs=eval("("+rs+")");
                $("#rs").html(rs);
            });
            $.post("/getUid","uname="+uname,function(bb){
                //一定要先转换成可以处理的js,否则是否等于null也无法判断
                bb=eval("("+bb+")");
                if (bb!=null){
                    $("#uid").html(bb.uid);
                    $("#money").html(bb.money);
                }else{
                    $("#uid").html("");
                    $("#money").html("");
                }
            });
        });
    })

</script>
<body>
    <%
        Cookie [] cookies = request.getCookies();
        String value = "";
        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("unameCookie")){
                    value = cookie.getValue();
                    break;
                }
            }
        }
        pageContext.setAttribute("unameCookie",value);
    %>
    <h1>请登录</h1>
    <form action="test" method="post">
        用户名:<input type="text" name="uname" id="uname" value="${unameCookie}" <%--onblur="test()"--%>><span id="rs"></span><br/>
        密码:<input type="password" name="upass"><br/>
        <input type="submit" value="登录">
    </form>
    账户id:<p id="uid"></p><br>
    账户余额：<p id="money"></p><br>
</body>
</html>
